package com.example.macbook.zarafa;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.com.zarafa.model.User;
import com.zarafa.util.Util;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.RequestSMSCodeListener;
import cn.bmob.v3.listener.SaveListener;

public class RegistActivity extends AppCompatActivity {

    @SuppressWarnings("unused")
    private static final String TAG = "RegisterActivity";

    private EditText editText_phone;
    private EditText editText_yanzhengma;
    private EditText editText_mima;
    private Button button_getyanzhengma;
    private Button button_regist;


    private  String  phonenum=null;
    private  String yanzhengma=null;
    private  String mima=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        editText_phone= (EditText) findViewById(R.id.phonenum);
        editText_yanzhengma= (EditText) findViewById(R.id.yanzhengma);
        editText_mima= (EditText) findViewById(R.id.mima);
        final Button getButton_getyanzhengma= (Button) findViewById(R.id.getyanzhengma);
        Button getButton_regist= (Button) findViewById(R.id.regist);
        Bmob.initialize(this, "1514c980513ad353c5385914bb5826ac");

        /**
         * 点击获取验证码
         */

        getButton_getyanzhengma.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              getyzm();
            }
        });

        /**
         * 点击注册
         */
         getButton_regist.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                regist();
                 Log.i("zz", mima);
                 Log.i("zz",yanzhengma);
                 Log.i("zz",phonenum);
             }
         });

    }

    /**
     * 输入手机号注册
     */
    public void getyzm() {
        phonenum = editText_phone.getText().toString();
        if (!Util.isPhoneNumberValid(phonenum)) {
            toast("手机号码有误");
        } {

            BmobSMS.requestSMSCode(this, phonenum, "短信模版", new RequestSMSCodeListener() {
                @Override
                public void done(Integer integer, BmobException e) {
                    if(e==null){
                        toast("验证码发送成");
                    }
                }
            });
        }
    }

    /**
     *登录
     */
    public void regist() {
        yanzhengma = editText_yanzhengma.getText().toString();
        mima = editText_mima.getText().toString();
        phonenum=editText_phone.getText().toString();


        if (!Util.isNetworkConnected(this)) {
            toast("网络异常");

        } else if(!Util.isPhoneNumberValid(phonenum)) {
            toast("手机号码有误");
        }else {
            User user = new User();
            user.setUsername(phonenum);
            user.setMobilePhoneNumber(phonenum);
            user.setPassword(mima);
            user.signOrLogin(this, yanzhengma, new SaveListener() {
                @Override
                public void onSuccess() {
                    toast("注册或登陆成功");
                }

                @Override
                public void onFailure(int i, String s) {
                    toast("错误码"+i+",错误原因" + s);
                }
            });

        }
    }


    public void toast(String toast) {
        Toast.makeText(this, toast, Toast.LENGTH_SHORT).show();
    };

}
